package com.base.moduleUser.strategy.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 登录策略工厂
 */
@Component
public class LoginStrategyFactory {

    private final Map<LoginType, LoginStrategy> strategyMap;

    @Autowired
    public LoginStrategyFactory(List<LoginStrategy> strategies) {
        // 初始化时将所有策略放入Map，key为登录类型
        this.strategyMap = strategies.stream()
                .collect(Collectors.toMap(
                        LoginStrategy::getLoginType,
                        Function.identity()
                ));
    }

    /**
     * 根据登录类型获取策略
     */
    public LoginStrategy getStrategy(LoginType loginType) {
        LoginStrategy strategy = strategyMap.get(loginType);
        if (strategy == null) {
            throw new UnsupportedOperationException("不支持的登录方式: " + loginType);
        }
        return strategy;
    }
}